﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    /// <summary>
    /// 积分操作类
    /// </summary>
    public class Integral_DAL : IIntegral
    {
        #region IIntegral 成员

        /// <summary>
        /// 增加积分
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Integral_Model model)
        {
            int Result = 0;
            db Conn = new db();
            string Sqlstr = " ";
            Sqlstr += " BEGIN TRANSACTION ";

            Sqlstr += " DECLARE @errorSum INT  ";
            Sqlstr += " DECLARE @Iscount INT  ";
            Sqlstr += "  Select  @Iscount=Count(id) from Integral_Log Where (DateDiff(dd,getdate(),addtime)=0) and  Userid=" + model.UserId + " and NetIp='" + StringHelper.Ip() + "' and types='" + model.Types + "' and Readme like '%" + model.Readme + "%'  ";
            Sqlstr += " SET @errorSum=0  ";
            Sqlstr += " if @Iscount<=0  begin  ";

            Sqlstr += " Update Users Set integral=integral+(" + model.Num + ") where Userid=" + model.UserId + "  ";
            Sqlstr += " Insert into Integral_Log(Num,UserId,Readme,NetIP,Types) values(" + model.Num + "," + model.UserId + ",'" + model.Readme + "','" + StringHelper.Ip() + "','" + model.Types + "')  ";
            //Sqlstr += " SET @errorSum=@errorSum+@@error ";
            //Sqlstr += " SET @errorSum=@errorSum+@@error  ";
            //Sqlstr += " IF @errorSum<>0  ";
            //Sqlstr += " BEGIN  ";
            //Sqlstr += " ROLLBACK TRANSACTION ";
            //Sqlstr += " END  ";
            //Sqlstr += " ELSE  ";
            //Sqlstr += " BEGIN  ";
            Sqlstr += "  end ";


            Sqlstr += " COMMIT TRANSACTION  ";
            //Sqlstr += " END  ";
            try
            {
                Result = Conn.ExcSql(Sqlstr);
            }
            catch
            {
                Result = -1;
            }
            finally
            {
                Conn.close();
            }
            return Result;

        }

        /// <summary>
        /// 扣除积分
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int subtract(Integral_Model model)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 删除积分记录
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public int del(string filter)
        {
            //string sql="BEGIN TRANSACTION  "
            throw new NotImplementedException();
        }

        #endregion
    }
}
